#include<iostream>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<string>

#define N 2
#define Num 1024

void Wirter(int Wpfd)
{
    std::string s="Hello Linux";
    char buf[Num];
    while(1)
    {
        buf[0]=0;
        getline(std::cin,s);
        write(Wpfd,s.c_str(),s.size());

        // scanf("%s",buf);
        // write(Wpfd,buf,strlen(buf));
        s.clear();
        sleep(1);
    }

}

void Reader(int Rpfd)
{
    char buf[Num];
    while(1)
    {
        //string s;
        ssize_t n=read(Rpfd,buf,sizeof(buf));
        //ssize_t n=read()
        if(n>0)
        {
            buf[n]=0;
            std::cout<<"I am father["<<getpid()<<"]"<<buf<<std::endl;
            
        }
        else
        {
            std::cout<<"done"<<std::endl;
        }
    }
}

int main()
{
    int pipefd[N]={0};
    pipe(pipefd);

    pid_t id=fork();
    if(id==0)
    {
        
        close(pipefd[0]);
        
        Wirter(pipefd[1]);
        
        close(pipefd[1]);
        exit(0);
    }

    close(pipefd[1]);
    
    Reader(pipefd[0]);

    int re=0;
    //pid_t t=waitpid(id,nullptr,0);
    pid_t rid = waitpid(id, &re, 0);

    return 0;
}